﻿/*
 *  This file is part of Worker Time.
 *
 *  Worker Time is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Worker Time is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Worker Time.  If not, see <http://www.gnu.org/licenses/>.
 */
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Common;
using System.Windows.Forms;
using WorkerTime.Utils;

namespace WorkerTime.Database
{
    class TrackUtils
    {
        public static Track GetTrack(Manager database, Int32 ID)
        {
            Track track;
            DbDataReader reader = database.ExecuteReader("SELECT * FROM Tracks WHERE ID='" + ID + "'");
            reader.Read();
            if (reader.HasRows)
            {
                track = new Track(Int32.Parse(reader["ProjectID"].ToString()), Int32.Parse(reader["ActivityID"].ToString()));
                track.ID = Int32.Parse(reader["ID"].ToString());
                track.startTime = (DateTime)reader["Start"];
                track.endTime = (DateTime)reader["End"];
            }
            else
            {
                throw new ApplicationException("Contagem não encontrada na base de dados");
            }
            reader.Close();
            return track;
        }

        public static void PopulateComboBoxProjects(Manager database, ComboBox comboBoxProject, String projectId)
        {
            PopulateComboBoxProjects(database,comboBoxProject);
            SelectComboBoxProjectItem(database,comboBoxProject, projectId);
        }

        public static void PopulateComboBoxProjects(Manager database, ComboBox comboBoxProject)
        {
            comboBoxProject.Items.Clear();
            comboBoxProject.Text = "";
            DbDataReader reader = database.ExecuteReader("SELECT ID,Name FROM Projects");
            while (reader.Read())
            {
                ComboListItem temp = new ComboListItem();
                temp.ShortName = reader[0].ToString();
                temp.LongName = (String)reader[1];
                comboBoxProject.Items.Add(temp);
            }
            reader.Close();
            if (comboBoxProject.Items.Count > 0)
                comboBoxProject.SelectedIndex = 0;
        }

        public static void SelectComboBoxProjectItem(Manager database, ComboBox comboBoxProject, String projectId)
        {
            for (int i = 0; i < comboBoxProject.Items.Count; i++)
            {
                if (((ComboListItem)comboBoxProject.Items[i]).ShortName == projectId)
                {
                    comboBoxProject.SelectedIndex = i;
                }
            }
        }

        public static void PopulateComboBoxActivities(Manager database, ComboBox comboBoxProject, ComboBox comboBoxActivity, String activityId)
        {
            PopulateComboBoxActivities(database, comboBoxProject, comboBoxActivity);
            SelectComboBoxActivityItem(database, comboBoxActivity, activityId);
        }

        public static void PopulateComboBoxActivities(Manager database, ComboBox comboBoxProject, ComboBox comboBoxActivity)
        {
            comboBoxActivity.Items.Clear();
            comboBoxActivity.Text = "";
            DbDataReader reader = database.ExecuteReader("SELECT ID,Name FROM Activity WHERE ProjectID=" + ((ComboListItem)comboBoxProject.Items[comboBoxProject.SelectedIndex]).ShortName);
            while (reader.Read())
            {
                ComboListItem temp = new ComboListItem();
                temp.ShortName = reader[0].ToString();
                temp.LongName = (String)reader[1];
                comboBoxActivity.Items.Add(temp);
            }
            reader.Close();
            if (comboBoxActivity.Items.Count > 0)
                comboBoxActivity.SelectedIndex = 0;
        }

        public static void SelectComboBoxActivityItem(Manager database, ComboBox comboBoxActivity, String activityId)
        {
            for (int i = 0; i < comboBoxActivity.Items.Count; i++)
            {
                if (((ComboListItem)comboBoxActivity.Items[i]).ShortName == activityId)
                {
                    comboBoxActivity.SelectedIndex = i;
                }
            }
        }
    }
}
